package cc.vv.baselibrary.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cc.vv.BaseNewApplication;
import cc.vv.baselibrary.bean.base.BaseResponseObj;
import cc.vv.baselibrary.global.BCIntentKey;
import cc.vv.baselibrary.global.BCSharePreKey;
import cc.vv.baselibrary.global.UserInfoSharePreKey;
import cc.vv.baselibrary.http.BaseHttpResponseKey;
import cc.vv.baselibrary.http.BaseHttpResponseObj;
import cc.vv.baselibrary.util.LKPrefUtil;
import cc.vv.baselibrary.util.router.RouterActivityIntentResourceKey;
import cc.vv.baselibrary.util.router.RouterTransferCenterUtil;
import cc.vv.baselibrary.vFinal.BTCodeKey;
import cc.vv.baselibrary.view.BCSingleButtonDialog;
import cc.vv.lkbasecomponent.base.ui.LKBaseActivity;
import cc.vv.lklibrary.log.LogOperate;

/**
 * 公共Activity基类
 *
 * @author chen.qi
 */
public class BoTuBaseActivity extends LKBaseActivity {

    protected Context mContext;

    private BaseNewApplication application;

    private BCSingleButtonDialog dialog;

    @Override
    protected void initView(@Nullable Bundle bundle) {
        mContext = this;
        if (application == null) {
            // 得到Application对象
            application = (BaseNewApplication) getApplication();
        }
        addActivity();
    }

    public void addActivity() {
        application.addActivity(this);
    }

    public void removeActivity(Activity activity) {
        application.removeActivity(activity);
    }

    public void exitApp() {
        application.exitApp();
    }

    public void exit(int num) {
        application.exit(num);
    }

    public void finishActivityAndRemove() {
        application.finishActivityAndRemove();
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            EventBus.getDefault().register(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        getLoading().closeLoading();
        try {
            //解注册
            EventBus.getDefault().removeAllStickyEvents();
            EventBus.getDefault().unregister(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onEvent(BaseHttpResponseObj event) {
        if (null == event) {
            return;
        }
        if (BaseHttpResponseKey.RESPONSE_TYPE_START.equals(event.getResponseType())) {
            //请求开始
            if (event.isLoading()) {
                getLoading().showLoading();
            }
        } else if (BaseHttpResponseKey.RESPONSE_TYPE_SUCCESS.equals(event.getResponseType())) {
            //请求成功success
            BaseResponseObj data = event.getData();
            if (null != data) {
                if (data.code == BTCodeKey.CODE_200) {
                    getData(data);
                } else {
                    if (data.code == BTCodeKey.CODE_300) {
                        //token失效
                        showDistanceLoginDialog();
                    } else {
                        getBadCode(data);
                    }
                }
            }
        } else if (BaseHttpResponseKey.RESPONSE_TYPE_FINISH.equals(event.getResponseType())) {
            //请求完成
            getLoading().closeLoading();
        } else if (BaseHttpResponseKey.RESPONSE_TYPE_FAILURE.equals(event.getResponseType())) {
            //请求失败
            getLoading().closeLoading();
            onHttpFailure(event.getUrl(), event.getExceptionStr());
        }
    }


    private void showDistanceLoginDialog() {
        if (null == dialog) {
            try {
                dialog = new BCSingleButtonDialog(this);
                dialog.setTitle("下线通知");
                dialog.setContent("你的账号在另一台设备登录，如非本人操作，则密码可能已泄露，请你修改密码。");
                dialog.setConfirmBtnText("重新登录");
                dialog.setEventInterface(new BCSingleButtonDialog.EventInterface() {
                    @Override
                    public void confirmOnClick() {
                        //跳转登录
                        exitApp();
                        dialog.dismiss();
                        LKPrefUtil.clearSP(UserInfoSharePreKey.USERINFO_DATA);
                        LKPrefUtil.clearSP(UserInfoSharePreKey.TOKEN);
                        Intent intent = RouterTransferCenterUtil.getInstance().getRouterIntent(mContext,
                                RouterActivityIntentResourceKey.KEY_MODULE_LOGIN);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        intent.putExtra(BCIntentKey.OUT_LINE, true);
                        startActivity(intent);
                    }
                });
            } catch (Exception e) {
                LogOperate.e(e.getMessage(), e);
            }
        }
        if (null != dialog && !dialog.isShowing()) {
            dialog.show();
        }
    }


    /**
     * 接口请求成功code值为非200和300时
     */
    protected void getBadCode(BaseResponseObj obj) {

    }

    /**
     * 接口请求成功回调
     */
    protected void getData(BaseResponseObj obj) {

    }

    /**
     * 接口请求成功回调
     */
    protected void onHttpFailure(String url, String exceptionStr) {

    }

}
